package org.geektimes.projects.user.service;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.repository.DatabaseUserRepository;
import org.geektimes.projects.user.repository.InMemoryUserRepository;
import org.geektimes.projects.user.repository.JpaDatabseUserRepository;
import org.geektimes.projects.user.sql.DBConnectionManager;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

public class UserServiceImpl implements UserService {
    @Resource(name = "bean/UserRepository")
    private JpaDatabseUserRepository repository;

    public void setRepository(JpaDatabseUserRepository repository) {
        this.repository = repository;
    }

    @Override
//    @LocalTransactional
    public boolean register(User user) {
        String password = Base64.getEncoder().encodeToString(user.getPassword().getBytes());
        User perUser = repository.getByNameAndPassword(user.getName(), password);
        if(perUser == null) {
            boolean save = repository.save(user);
//            int i = 1/0;
            return save;
        }else{
            return false;
        }
    }

    @Override
    public boolean login(User user) {
        String password = Base64.getEncoder().encodeToString(user.getPassword().getBytes());
        User perUser = repository.getByNameAndPassword(user.getName(), password);
        if(perUser != null) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deregister(User user) {
        return false;
    }

    @Override
    public boolean update(User user) {
        return false;
    }

    @Override
    public User queryUserById(Long id) {
        return repository.getById(id);
    }

    @Override
    public User queryUserByNameAndPassword(String name, String password) {
        return null;
    }

}
